package com.yowasa.xiuxian.service.helper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.po.XiuXianExtend;
import com.yowasa.xiuxian.mapper.XiuxianExtendMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class ExtendHelper {

    @Autowired
    private XiuxianExtendMapper xiuxianExtendMapper;

    public XiuXianExtend getByAccountId(Long accountId){
        XiuXianExtend xiuXianExtend = xiuxianExtendMapper.selectOne(new LambdaQueryWrapper<XiuXianExtend>().eq(XiuXianExtend::getAccountId, accountId).last("limit 1"));
        if(xiuXianExtend == null){
            xiuXianExtend = new XiuXianExtend(accountId);
            xiuxianExtendMapper.insert(xiuXianExtend);
        }
        return xiuXianExtend;
    }

    public void  updateExtendById(XiuXianExtend extend){
        xiuxianExtendMapper.updateById(extend);
    }

    public boolean checkSign(Long id) {
        if(xiuxianExtendMapper.checkSign(id) == 0){
            return true;
        }
        return false;
    }

    public Integer getSignNum(Long id) {
        return xiuxianExtendMapper.getSignNum(id);
    }

    public void insertSign(Long id) {
        xiuxianExtendMapper.insertSign(id);
    }

    public Integer insertOrUpdateDamage(UserBO userBO, int i) {
        Map<String, Object> map = xiuxianExtendMapper.getTodayDamage(userBO.getId());
        if(map == null){
            xiuxianExtendMapper.insertDamage(userBO.getId(),i,userBO.getStage());
            return 1;
        }else {

            Integer level = (Integer) map.get("level");
            Integer damage = (Integer) map.get("damage");

            if(userBO.getStage().equals(level) && damage < i ){
                xiuxianExtendMapper.updateDamage(userBO.getId(),i);
            }
            return  2;
        }

    }

    public List<Map<String, Object>> listDamage(int level) {
        return xiuxianExtendMapper.listDamage(level);
    }

    public void refreshAbyss() {
        xiuxianExtendMapper.refreshAbyss();
    }
}
